/**
 * Created by apple on 2018/2/23.
 */
module.exports = function (grunt) {
    //// Project configuration
    grunt.initConfig({
        'pkg': grunt.file.readJSON('package.json'),
        "sass": {
            options: {
                style: 'compressed'
            },
            dist: {
                expand: true,
                cwd: 'static/css',
                src: ['**/*.scss'],
                dest: 'static/css',
                ext: '.css'
            },
            watch: {}
        },
        'cssmin': {
            options: {
                sourceMap: true
            },
            skin: {
                src: ['static/css/skin/**/*.css', '!static/css/skin/*.min.css'],
                dest: 'static/css/skin/all.min.css'
            }
        },
        'sprite': {
            'ui-icon': {
                src: 'static/image/sprite/**/*.png',
                dest: 'static/images/sprite.png',
                destCss: 'static/css/common/_sprite.scss',
                imgPath: '../../static/images/sprite.png',
                algorithm: 'binary-tree', //op-down/left-right/diagonal/alt-diagonal/binary-tree
                cssFormat: 'scss',
                cssVarMap: function (sprite) {
                    sprite.name = sprite.name;
                }
            }
        },
        'uglify': {
            options: {
                mangle: false,
                sourceMap: true,
                output: {
                    comments: false
                }
            },
            dist: {
                expand: true,
                cwd: 'static/js/base',
                src: ['**/*.js', '!**/*.min.js'],
                dest: 'static/js/base',
                ext: '.min.js'
            },
            watch: {}
        },
        'concat': {
            options: {
                separator: '\n\n',
                stripBanners: true,
                sourceMap: true
            },
            dist: {
                src: [
                    'static/js/base/jquery.min.js',
                    'static/js/base/jquery-cookie.min.js',
                    'static/js/base/jquery-pjax.min.js',
                    'static/js/base/require.min.js',
                    'static/js/base/kaisheng.min.js',
                    'static/js/base/expand.min.js'
                ],
                dest: 'static/js/base.min.js'
            }
        },
        'watch': {
            options: {
                spawn: false
            },
            'sass': {
                files: '**/*.scss'
                //,tasks: ['sass:watch']
            },
            'alljs': {
                files: ['static/js/**/*.js', '!static/js/**/*.min.js']
            }
        },
        'htmlstamp': {
            options: {
                type: 'inline',
                src: 'WEB-INF/jsp/',
                dest: 'WEB-INF/jsp/',
                patten: '**/*.jsp',
                comboCss: ['static/**/*.css', 'static/**/*.js']
            },
            suffix_time: {
                //files: {'../WEB-INF/jsp/acccoupon/**.jsp':['../static/js/bootstrap.min.js']}
            }
        },
        //'cache': cacheOptions
    });

    // 监听事件
    grunt.event.on('watch', function (action, filepath, target) {
        if (action == "changed" && target === "sass") {
            var sasspath = filepath,
                csspath = filepath.replace('.scss', '') + '.css';
            var filesObj = {};
            filesObj[csspath] = sasspath;
            grunt.config("sass.watch.files", filesObj);
            if (filepath.indexOf('components') > -1) {
                grunt.task.run(['sass:watch', 'cssmin']);
            } else {
                grunt.task.run('sass:watch');
            }
        } else if (action == "changed" && target === "alljs") {
            var jspath = filepath,
                minjspath = filepath.replace('.js', '') + '.min.js';
            var jsfilesObj = {};
            jsfilesObj[minjspath] = jspath;
            grunt.config("uglify.watch.files", jsfilesObj);

            if (true) return;//js开发阶段不压缩

            if (filepath.indexOf('base') > -1) {
                grunt.task.run(['uglify:watch', 'concat']);
            } else {
                grunt.task.run('uglify:watch');
            }
        }
    });

    // 加载插件
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-spritesmith');
    grunt.loadNpmTasks('grunt-htmlstamp');

    // 自定义任务
    grunt.registerTask('watch-change', [
        'watch'
    ]);
    grunt.registerTask('build', [
        'build-js', 'build-style'
    ]);
    grunt.registerTask('build-style', ['sprite:ui-icon', 'sass:dist', 'cssmin']);
    grunt.registerTask('build-js', ['uglify:dist', 'concat']);
    grunt.registerTask('build-version', ['htmlstamp']);
}